package com.zhimu.activemq_demo.activemq.topic;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * @Author: Alvin
 * @Date: 2019/6/22 11:08
 * @Description
 */
public class JmsConsumerTopicPersist {
    public static final String ACTIVE_URL = "tcp://127.0.0.1:61616";

    public static final String TOPIC_NAME = "topic_persist";

    public static void main(String[] args) throws JMSException, IOException {

        System.out.println("********z3 .....");

        // 1、创建连接工厂,按照给定URL，默认账号密码
        ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory(ACTIVE_URL);
        // 2、通过连接工厂，获得connection，并启动访问
        Connection connection = connectionFactory.createConnection();
        connection.setClientID("z3");
        connection.start();
        // 3、创建会话Session
        // 两个参数，第一个叫事务，第二个叫签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        // 4、创建目的地（具体是队列还是主题）
        Topic topic = session.createTopic(TOPIC_NAME);
        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic, "remark .....");


        Message message = topicSubscriber.receive();
        while (null != message) {
            TextMessage textMessage = (TextMessage) message;
            System.out.println("收到持久化topic：" + textMessage.getText());
            message = topicSubscriber.receive();
        }

        session.close();
        connection.close();
        System.out.println("*******************消费者接收信息完成");


    }
}
